#!/usr/bin/env python
# encoding: utf-8
import random
import string
import requests


def generate_verify_code(length=6):
    return ''.join(random.sample(string.digits, length))


def yunpian_send_sms(phone, verify_code):
    res = requests.post('http://yunpian.com/v1/sms/send.json', {
        'apikey': 'b7b7d55428334ac62f3289a4e47ae828',
        'mobile': phone,
        'text': '【城市达人】您的验证码是' + verify_code,
    })

    if res.status_code != 200:
        return None

    res_content = res.json()

    if 'code' in res_content and res_content['code'] == 0:
        return True
    else:
        return False

if __name__ == '__main__':
    yunpian_send_sms('18515158615', generate_verify_code())